package com.huawei.hms.utils;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.ContainerUtils;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.support.log.common.Base64;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;

public class b {
    public static boolean a(X509Certificate x509Certificate, List<X509Certificate> list) {
        GeneralSecurityException e;
        if (list.size() == 0) {
            return false;
        }
        PublicKey publicKey = x509Certificate.getPublicKey();
        PublicKey publicKey2 = publicKey;
        for (X509Certificate x509Certificate2 : list) {
            try {
                x509Certificate2.verify(publicKey2);
                publicKey2 = x509Certificate2.getPublicKey();
            } catch (CertificateException e2) {
                e = e2;
            } catch (NoSuchAlgorithmException e3) {
                e = e3;
            } catch (InvalidKeyException e4) {
                e = e4;
            } catch (NoSuchProviderException e5) {
                e = e5;
            } catch (SignatureException e6) {
                e = e6;
            }
        }
        return b((List) list);
        HMSLog.e("X509CertUtil", "verify failed " + e.getMessage());
        return false;
    }

    public static X509Certificate a(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return a(Base64.decode(str));
    }

    public static X509Certificate a(byte[] bArr) {
        try {
            return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(bArr));
        } catch (CertificateException e) {
            HMSLog.e("X509CertUtil", "Failed to get cert: " + e.getMessage());
            return null;
        }
    }

    public static List<X509Certificate> a(List<String> list) {
        List<X509Certificate> arrayList = new ArrayList(list.size());
        for (String a : list) {
            arrayList.add(a(a));
        }
        return arrayList;
    }

    public static List<X509Certificate> b(String str) {
        return a(c(str));
    }

    private static List<String> c(String str) {
        try {
            JSONArray jSONArray = new JSONArray(str);
            if (jSONArray.length() <= 1) {
                return Collections.emptyList();
            }
            List<String> arrayList = new ArrayList(jSONArray.length());
            for (int i = 0; i < jSONArray.length(); i++) {
                arrayList.add(jSONArray.getString(i));
            }
            return arrayList;
        } catch (JSONException e) {
            HMSLog.e("X509CertUtil", "Failed to getCertChain: " + e.getMessage());
            return Collections.emptyList();
        }
    }

    private static String a(String str, String str2) {
        int indexOf = str.toUpperCase(Locale.getDefault()).indexOf(str2 + ContainerUtils.KEY_VALUE_DELIMITER);
        if (indexOf == -1) {
            return null;
        }
        int indexOf2 = str.indexOf(",", indexOf);
        if (indexOf2 != -1) {
            return str.substring((indexOf + str2.length()) + 1, indexOf2);
        }
        return str.substring((indexOf + str2.length()) + 1);
    }

    public static boolean a(X509Certificate x509Certificate) {
        if (x509Certificate == null) {
            return false;
        }
        return x509Certificate.getKeyUsage()[5];
    }

    public static boolean b(List<X509Certificate> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            if (!a((X509Certificate) list.get(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean a(X509Certificate x509Certificate, String str, String str2) {
        if (x509Certificate == null || TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return false;
        }
        return str2.equals(a(x509Certificate.getSubjectDN().getName(), str));
    }

    public static boolean a(X509Certificate x509Certificate, String str) {
        return a(x509Certificate, "CN", str);
    }

    public static boolean b(X509Certificate x509Certificate, String str) {
        return a(x509Certificate, "OU", str);
    }

    public static boolean b(X509Certificate x509Certificate, String str, String str2) {
        try {
            return a(x509Certificate, str.getBytes("UTF-8"), Base64.decode(str2));
        } catch (UnsupportedEncodingException e) {
            HMSLog.e("X509CertUtil", " plainText exception: " + e.getMessage());
            return false;
        }
    }

    public static boolean a(X509Certificate x509Certificate, byte[] bArr, byte[] bArr2) {
        GeneralSecurityException e;
        try {
            Signature instance = Signature.getInstance(x509Certificate.getSigAlgName());
            instance.initVerify(x509Certificate.getPublicKey());
            instance.update(bArr);
            return instance.verify(bArr2);
        } catch (NoSuchAlgorithmException e2) {
            e = e2;
            HMSLog.e("X509CertUtil", "failed checkSignature : " + e.getMessage());
            return false;
        } catch (SignatureException e3) {
            e = e3;
            HMSLog.e("X509CertUtil", "failed checkSignature : " + e.getMessage());
            return false;
        } catch (InvalidKeyException e4) {
            e = e4;
            HMSLog.e("X509CertUtil", "failed checkSignature : " + e.getMessage());
            return false;
        }
    }

    public static X509Certificate a(Context context, String str) {
        InputStream open;
        Exception e;
        Throwable th;
        if (context == null || TextUtils.isEmpty(str)) {
            HMSLog.e("X509CertUtil", "args are error");
            return null;
        }
        try {
            KeyStore instance = KeyStore.getInstance("bks");
            open = context.getAssets().open("updatesdkcas.bks");
            try {
                instance.load(open, "".toCharArray());
                if (instance.containsAlias(str)) {
                    Certificate certificate = instance.getCertificate(str);
                    if (certificate instanceof X509Certificate) {
                        X509Certificate x509Certificate = (X509Certificate) certificate;
                        IOUtils.closeQuietly(open);
                        return x509Certificate;
                    }
                    IOUtils.closeQuietly(open);
                    return null;
                }
                HMSLog.e("X509CertUtil", "Not include alias " + str);
                IOUtils.closeQuietly(open);
                return null;
            } catch (KeyStoreException e2) {
                e = e2;
                try {
                    HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
                    IOUtils.closeQuietly(open);
                    return null;
                } catch (Throwable th2) {
                    th = th2;
                    IOUtils.closeQuietly(open);
                    throw th;
                }
            } catch (IOException e3) {
                e = e3;
                HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
                IOUtils.closeQuietly(open);
                return null;
            } catch (CertificateException e4) {
                e = e4;
                HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
                IOUtils.closeQuietly(open);
                return null;
            } catch (NoSuchAlgorithmException e5) {
                e = e5;
                HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
                IOUtils.closeQuietly(open);
                return null;
            }
        } catch (KeyStoreException e6) {
            e = e6;
            open = null;
            HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
            IOUtils.closeQuietly(open);
            return null;
        } catch (IOException e7) {
            e = e7;
            open = null;
            HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
            IOUtils.closeQuietly(open);
            return null;
        } catch (CertificateException e8) {
            e = e8;
            open = null;
            HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
            IOUtils.closeQuietly(open);
            return null;
        } catch (NoSuchAlgorithmException e9) {
            e = e9;
            open = null;
            HMSLog.e("X509CertUtil", "exception:" + e.getMessage());
            IOUtils.closeQuietly(open);
            return null;
        } catch (Throwable th3) {
            th = th3;
            open = null;
            IOUtils.closeQuietly(open);
            throw th;
        }
    }

    public static X509Certificate a(Context context) {
        return a(context, "052root");
    }
}
